package cn.iocoder.yudao.module.promotion.controller.admin.updateorder;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.promotion.controller.admin.updateorder.vo.*;
import cn.iocoder.yudao.module.promotion.dal.dataobject.updateorder.UpdateOrderDO;
import cn.iocoder.yudao.module.promotion.service.updateorder.UpdateOrderService;

@Tag(name = "管理后台 - 升级订单")
@RestController
@RequestMapping("/promotion/update-order")
@Validated
public class UpdateOrderController {

    @Resource
    private UpdateOrderService updateOrderService;

    @PostMapping("/create")
    @Operation(summary = "创建升级订单")
    @PreAuthorize("@ss.hasPermission('promotion:update-order:create')")
    public CommonResult<Long> createUpdateOrder(@Valid @RequestBody UpdateOrderSaveReqVO createReqVO) {
        return success(updateOrderService.createUpdateOrder(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新升级订单")
    @PreAuthorize("@ss.hasPermission('promotion:update-order:update')")
    public CommonResult<Boolean> updateUpdateOrder(@Valid @RequestBody UpdateOrderSaveReqVO updateReqVO) {
        updateOrderService.updateUpdateOrder(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除升级订单")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('promotion:update-order:delete')")
    public CommonResult<Boolean> deleteUpdateOrder(@RequestParam("id") Long id) {
        updateOrderService.deleteUpdateOrder(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得升级订单")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('promotion:update-order:query')")
    public CommonResult<UpdateOrderRespVO> getUpdateOrder(@RequestParam("id") Long id) {
        UpdateOrderDO updateOrder = updateOrderService.getUpdateOrder(id);
        return success(BeanUtils.toBean(updateOrder, UpdateOrderRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得升级订单分页")
    @PreAuthorize("@ss.hasPermission('promotion:update-order:query')")
    public CommonResult<PageResult<UpdateOrderRespVO>> getUpdateOrderPage(@Valid UpdateOrderPageReqVO pageReqVO) {
        PageResult<UpdateOrderDO> pageResult = updateOrderService.getUpdateOrderPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, UpdateOrderRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出升级订单 Excel")
    @PreAuthorize("@ss.hasPermission('promotion:update-order:export')")
    @OperateLog(type = EXPORT)
    public void exportUpdateOrderExcel(@Valid UpdateOrderPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<UpdateOrderDO> list = updateOrderService.getUpdateOrderPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "升级订单.xls", "数据", UpdateOrderRespVO.class,
                        BeanUtils.toBean(list, UpdateOrderRespVO.class));
    }

}